const mysql = require('mysql');

class TMysqlClients {
    constructor(){
        console.log('create object <-- TMySqlClients');

        this.clients = {}
    }
    setClientItem ( code , host , port , db , uid , pwd ){
        var me = this;
        try {
            var item = new TMySqlClientItem(code , host , port , db , uid , pwd)    
            me.clients[item.code] = item
        } catch (error) {
            console.log(error)
        }
    }
    getItem(code){
        var me = this;
        var item = null
        try {
            if (me.clients[code]){
                item = me.clients[code]
            }
        } catch (error) {
            console.log(error)
        }
        return item;
    }
}

class TMySqlClientItem {
    constructor( code , host , port , db , uid , pwd ){
        this.code = code;
        this.host = host;
        this.port = port;
        this.db = db;
        this.uid = uid;
        this.pwd = pwd;
        this.pool = null
    }

    getPool(){
        var me = this;
        try{
            if ( me.pool==null){
                me.pool = mysql.createPool({
                    host: me.host ,
                    port : me.port ,
                    database: me.db ,
                    user: me.uid ,
                    password: me.pwd
                })
            }
        }
        catch(error){
            console.log(error)
        }
        return me.pool;
    }

    getConntion(){
        var me = this;
        return new Promise((success, fail)=>{
            try {
                me.getPool();
                me.pool.getConnection((err , conn)=>{
                    if ( err ){
                        console.log('get connection fail');
                        success(null)
                    }
                    else{
                        conn.config.queryFormat = function (query, values) {
                            if (!values) return query;
                            return query.replace(/\:(\w+)/g, function (txt, key) {
                                var vStr = txt
                                if (values.hasOwnProperty(key)) {
                                    vStr = this.escape(values[key]);
                                }
                                return vStr;
                                //if (values.hasOwnProperty(key)) {
                                //    return this.escape(values[key]);
                                //}
                                //return txt;
                            }.bind(this));
                        };
                        success(conn)
                    }
                })
            } catch (error) {
                console.log(error);
                success(null);
            }
        })
    }

    query( sql , params){
        var me = this;
        return new Promise((success, fail)=>{
            try {
                me.getConntion().then( _conn=>{
                    if ( _conn==null){
                        console.log('query fail. (connection is null)')
                        success(null);
                    }
                    else{
                        _conn.query( sql , params , (_err , _result)=>{
                            if (_err){
                                console.log( 'query fail.');
                                success(null)
                            }
                            else{
                                var res = JSON.parse(JSON.stringify(_result))
                                success(res)
                            }
                        });
                    }
                });
            } catch (error) {
                console.log(error);
                success(null)
            }
        })
    }
}

const dbClients = new TMysqlClients;

dbClients.setClientItem("hosdb", "dev.yy-sport.com.cn", 31023 , "hosdb", "root", "Hhuc1115" )
module.exports = {
    db:dbClients
}
